using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;
using Kypris2.CoreElements;
using Jamila2.Tools;

public partial class GonzalesUserOption : System.Web.UI.Page
{
    protected PrivateLoginSession CurrentPrivateSession = null;
    protected LogManager CurLogManager = null;

    // response text

    #region Response_Done
    public string Response_Done
    {
        get
        {
            return ViewState["Response_Done"].ToString();
        }

        set
        {
            ViewState["Response_Done"] = value;
        }
    }
    #endregion

    #region Response_Error
    public string Response_Error
    {
        get
        {
            return ViewState["Response_Error"].ToString();
        }

        set
        {
            ViewState["Response_Error"] = value;
        }
    }
    #endregion

    #region Response_Cancel
    public string Response_Cancel
    {
        get
        {
            return ViewState["Response_Cancel"].ToString();
        }

        set
        {
            ViewState["Response_Cancel"] = value;
        }
    }
    #endregion

    #region Events

    #region Page_Load
    protected void Page_Load(object sender, EventArgs e)
    {
        this.SessionCheck();    // first thing to do for every page

        if (!IsPostBack)
        {
            this.SwitchLanguage();
            this.PageComponentSetup();            
            
        }
    }
    #endregion

    #region Button_Submit_Click
    protected void Button_Submit_Click(object sender, EventArgs e)
    {
        this.Label_Response.Visible = false;

        using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
        {
            try
            {
                this.Label_Response.Visible = true;

                if (CurUserManager.UpdatePersonnelParticularInOption(
                    this.CurrentPrivateSession.CurrentLoggedOnUser.InternalID,
                    "",
                    this.DropDownList_Language.SelectedValue,
                    this.DropDownList_DateFormat.SelectedValue))
                {
                    // log down the history
                    this.CurLogManager.LogMessage("[GonzalesUserOption] : User detail (" + this.CurrentPrivateSession.CurrentLoggedOnUser.InternalID + ", " + this.DropDownList_Language.SelectedValue + ", " + this.DropDownList_DateFormat.SelectedValue  + ") updated", this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);

                    this.Label_Response.CssClass = "MessageInformation";
                    this.Label_Response.Text = this.Response_Done;   
                 
                    // reset in the session                    
                    this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage = new Language(this.DropDownList_Language.SelectedValue, this.DropDownList_Language.SelectedItem.Text);

                    switch (this.DropDownList_DateFormat.SelectedValue)
                    {
                        case "DDMMYYYY":
                            this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedDateFormat = TypeOfDateFormat.DDMMYYYY;
                            break;

                        case "MMDDYYYY":
                            this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedDateFormat = TypeOfDateFormat.MMDDYYYY;
                            break;

                        case "YYYYDDMM":
                            this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedDateFormat = TypeOfDateFormat.YYYYDDMM;
                            break;

                        case "YYYYMMDD":
                            this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedDateFormat = TypeOfDateFormat.YYYYMMDD;
                            break;
                    }

                    Session[GonzalesCommonNamingConvention.CurrentPrivateSession] = this.CurrentPrivateSession;
                }
                else
                {
                    // error
                    this.Label_Response.CssClass = "MessageError";
                    this.Label_Response.Text = this.Response_Error;
                }
            }
            catch (Exception ex)
            {
                using (LogManager CurLogManager = new LogManager(this.CurrentPrivateSession))
                {
                    CurLogManager.LogError("[GonzalesUserOption.Button_Submit_Click.EX] : " + ex.Message, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);
                }
            }
        }
    }
    #endregion

    #region DropDownList_Language_SelectedIndexChanged
    protected void DropDownList_Language_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.SwitchLanguage();
        this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage = new Language(this.DropDownList_Language.SelectedValue, this.DropDownList_Language.SelectedItem.Text);
        Session[GonzalesCommonNamingConvention.CurrentPrivateSession] = this.CurrentPrivateSession;
    }
    #endregion

    #region ImageButton_Cancel_Click
    protected void ImageButton_Cancel_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("GonzalesMain.aspx");
    }
    #endregion

    #endregion

    #region Customized Methods

    #region SessionCheck
    public void SessionCheck()
    {

        if (Session[GonzalesCommonNamingConvention.CurrentPrivateSession] != null)
        {
            this.CurrentPrivateSession = (PrivateLoginSession)Session[GonzalesCommonNamingConvention.CurrentPrivateSession];
            this.CurLogManager = new LogManager(this.CurrentPrivateSession);    // activate Log Manager
        }
        else
        {
            Response.Redirect(GonzalesErrorPageRedirect.SessionError);
        }

    }
    #endregion

    #region PageComponentSetup
    protected void PageComponentSetup()
    {
        // language dropdown
        using (LanguageManager CurLanguageManager = new LanguageManager(this.CurrentPrivateSession))
        {
            MasterBaseCollection ResultCollection = CurLanguageManager.GetAllLanguageInOrder(true);
            if (ResultCollection != null && ResultCollection.Count > 0)
            {
                this.DropDownList_Language.Items.Clear();

                for (int x = 0; x < ResultCollection.Count; x++)
                {
                    this.DropDownList_Language.Items.Add(new ListItem(ResultCollection[x].DisplayName, ResultCollection[x].InternalID));
                }
            }
        }

        try
        {
            this.DropDownList_Language.SelectedValue = this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID;
        }
        catch (Exception ex)
        {
            using (LogManager CurLogManager = new LogManager(this.CurrentPrivateSession))
            {
                CurLogManager.LogError("[GonzalesUserOption.PageComponentSetup.EX] : " + ex.Message, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);
            }
        }

        // date format
        this.DropDownList_DateFormat.Items.Add(new ListItem("DD/MM/YYYY", TypeOfDateFormat.DDMMYYYY.ToString()));
        this.DropDownList_DateFormat.Items.Add(new ListItem("MM/DD/YYYY", TypeOfDateFormat.MMDDYYYY.ToString()));
        this.DropDownList_DateFormat.Items.Add(new ListItem("YYYY/DD/MM", TypeOfDateFormat.YYYYDDMM.ToString()));
        this.DropDownList_DateFormat.Items.Add(new ListItem("YYYY/MM/DD", TypeOfDateFormat.YYYYMMDD.ToString()));

        this.DropDownList_DateFormat.SelectedValue = this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedDateFormat.ToString();

        // javascript
        this.ImageButton_Cancel.Attributes["onclick"] = "return confirm('" + this.Response_Cancel + "');";

    }
    #endregion

    #region SwitchLanguage
    protected void SwitchLanguage()
    {
        MultiLingualFactory CurLanguageFactory = null;

        if (!IsPostBack)
        {
            CurLanguageFactory = MultiLingualEngine.GetMultiLingualFactory(this, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID);
        }
        else
        {   
            // is already a post back, will follow the drop down
            CurLanguageFactory = MultiLingualEngine.GetMultiLingualFactory(this, this.DropDownList_Language.SelectedValue);
        }

        using (CurLanguageFactory)
        {
            this.Label_Information.Text = CurLanguageFactory.DisplayValue("Information");
            this.Label_PageTitle.Text = CurLanguageFactory.DisplayValue("PageTitle");
            this.Label_Remarks.Text = CurLanguageFactory.DisplayValue("Remarks");            
            this.Label_Language.Text = CurLanguageFactory.DisplayValue("Label_Language");
            this.Label_DateFormat.Text = CurLanguageFactory.DisplayValue("Label_DateFormat");
            this.Button_Submit.Text = CurLanguageFactory.DisplayValue("Button_Submit");
            this.Response_Done = CurLanguageFactory.DisplayValue("ResponseDone");
            this.Response_Error = CurLanguageFactory.DisplayValue("ResponseError");
            this.Response_Cancel = CurLanguageFactory.DisplayValue("ResponseCancel");
        }


    }
    #endregion

    #endregion



}
